﻿/* 
 * Copyright (c) 2018, Firely (info@fire.ly) and contributors
 * See the file CONTRIBUTORS for details.
 * 
 * This file is licensed under the BSD 3-Clause license
 * available at https://github.com/FirelyTeam/firely-net-sdk/blob/master/LICENSE
 */

using Hl7.Fhir.Model;
using System.Collections.Generic;

namespace Hl7.Fhir.Specification.Source
{
    /// <summary>Extension methods for the <see cref="ISummarySource"/> interface.</summary>
    public static class SummarySourceExtensions
    {
        /// <summary>Load the resource from which the specified summary was generated.</summary>
        /// <param name="source">A <see cref="ISummarySource"/> instance.</param>
        /// <param name="summary">An <see cref="ArtifactSummary"/> instance generated by this source.</param>
        /// <typeparam name="T">The resource type to return.</typeparam>
        /// <returns>A new resource instance of type <typeparamref name="T"/>, or <c>null</c>.</returns>
        public static T LoadBySummary<T>(this ISummarySource source, ArtifactSummary summary) where T : Resource
            => source.LoadBySummary(summary) as T;

        /// <summary>Returns a list of <see cref="ArtifactSummary"/> instances with error information.</summary>
        /// <param name="source">A <see cref="ISummarySource"/> instance.</param>
        /// <returns>A sequence of <see cref="ArtifactSummary"/> instances.</returns>
        public static IEnumerable<ArtifactSummary> ListSummaryErrors(this ISummarySource source)
            => source.ListSummaries().Errors();

        /// <summary>Returns a list of <see cref="ArtifactSummary"/> instances for resources of the specified resourceTypeName.</summary>
        /// <param name="source">A <see cref="ISummarySource"/> instance.</param>
        /// <param name="resourceTypeName">name of the Resource</param>
        /// <returns>A sequence of <see cref="ArtifactSummary"/> instances.</returns>
        public static IEnumerable<ArtifactSummary> ListSummaries(this ISummarySource source, string resourceTypeName)
            => source.ListSummaries().OfResourceType(resourceTypeName);
    }
}
